Imports System.IO
Imports System.Diagnostics
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient

Partial Class RunOne
  Inherits System.Web.UI.Page
  Dim PerlsDir As String = ConfigurationManager.AppSettings("PerlsDirectory")

  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' get the scraper program name
    Dim mySqlConnection As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("VendorStatsConnectionString").ToString)
    Dim mySqlCommand As New SqlCommand
    Dim mySqlDataReader As SqlDataReader
    mySqlConnection.Open()

    mySqlCommand.CommandType = CommandType.Text
    mySqlCommand.Connection = mySqlConnection
    mySqlCommand.CommandText = "SELECT scraper FROM Vendors WHERE VendorId = " & ddlVendors.SelectedValue
    mySqlDataReader = mySqlCommand.ExecuteReader()
    mySqlDataReader.Read()
    Dim scraper As String = mySqlDataReader.GetString(0)
    mySqlDataReader.Close()
    mySqlConnection.Close()

    ' Create the ProcessInfo object
    Dim psi As New ProcessStartInfo("cmd.exe")
    psi.UseShellExecute = False
    psi.RedirectStandardOutput = True
    psi.RedirectStandardInput = True
    psi.RedirectStandardError = True

    ' Start the process
    Dim proc As Process = Process.Start(psi)
    AddHandler proc.OutputDataReceived, AddressOf ProcessOutputHandler
    AddHandler proc.ErrorDataReceived, AddressOf ProcessOutputHandler

    ' write the commands to the process
    Dim sw As StreamWriter = proc.StandardInput
    Dim perlCommand As String
    sw.WriteLine(PerlsDir.Substring(0, 2))
    sw.WriteLine("cd " & PerlsDir)
    perlCommand = "perl " & scraper & " " & ddlStartDate.SelectedValue & " " & ddlEndDate.SelectedValue
    If ddlColleges.SelectedValue > 0 Then
      perlCommand = perlCommand & " " & ddlColleges.SelectedValue
      If ddlCollegeThru.SelectedValue > 0 Then
        perlCommand = perlCommand & "-" & ddlCollegeThru.SelectedValue
      End If
    End If
    sw.WriteLine(perlCommand)
    sw.WriteLine("exit")

    ' display whatever comes back.
    Response.Buffer = False
    proc.BeginOutputReadLine()
    proc.BeginErrorReadLine()
    proc.WaitForExit()

    proc.Close()
    sw.Close()

  End Sub
  Protected Sub ProcessOutputHandler(ByVal sendingProcess As Object, _
  ByVal outLine As DataReceivedEventArgs)

    ' Collect the sort command output.
    If Not String.IsNullOrEmpty(outLine.Data) Then
      Response.Write(outLine.Data & "<br>")
      Response.Flush()
    End If
  End Sub
  Protected Sub ddlStartDate_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlStartDate.Init
    Dim myDate As New Date(Today().Year, Today().Month, Today().AddMonths(1).AddDays(-1).Day)
    Dim i As Integer
    myDate = Today
    For i = 0 To 23
      myDate = myDate.AddMonths(-1)
      ddlStartDate.Items.Insert(i, New ListItem(myDate.Month & "-" & myDate.Year, myDate.Year & "-" & myDate.Month & "-1"))
    Next
  End Sub

  Protected Sub ddlEndDate_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlEndDate.Init
    Dim myDate As New Date(Today().Year, Today().Month, Today().AddMonths(1).AddDays(-1).Day)
    Dim i As Integer
    myDate = Today
    For i = 0 To 23
      myDate = myDate.AddMonths(-1)
      ddlEndDate.Items.Insert(i, New ListItem(myDate.Month & "-" & myDate.Year, myDate.Year & "-" & myDate.Month & "-1"))
    Next
  End Sub

  Protected Sub ddlColleges_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlColleges.DataBound
    ddlColleges.Items.Insert(0, New ListItem("All Colleges", 0))
  End Sub
  Protected Sub ddlCollegeThru_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCollegeThru.DataBound
    ddlCollegeThru.Items.Insert(0, New ListItem("All Colleges", 0))
  End Sub
End Class